package com.example.lib.utils.permission

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.example.lib.constants.JumpConstants
import com.example.lib.utils.DeviceUtils
import java.io.File

object RequestPermission {
    private var mApkFile: File? = null

    fun check(activity: Activity, vararg permissions: String): Boolean {
        return XXPermissions.isGranted(activity, permissions)
    }

    fun requestCamera(activity: Activity, result: (Boolean) -> Unit) {
        XXPermissions.with(activity).permission(Permission.CAMERA).request(object : OnPermissionCallback {
            override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                if (allGranted) result.invoke(true)
                else result.invoke(false)
            }

            override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                result.invoke(false)
            }
        })
    }

    fun requestLocation(activity: Activity, result: (Boolean) -> Unit) {
        if (check(activity, Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)) {
            result.invoke(true)
        } else {
            XXPermissions.with(activity).permission(Permission.ACCESS_FINE_LOCATION, Permission.ACCESS_COARSE_LOCATION)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) result.invoke(true)
                        else result.invoke(false)
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        result.invoke(false)
                    }
                })
        }
    }

    fun requestAudio(activity: Activity, result: (Boolean) -> Unit) {
        if (check(activity, Permission.RECORD_AUDIO)) {
            result.invoke(true)
        } else {
            XXPermissions.with(activity).permission(Permission.RECORD_AUDIO)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) result.invoke(true)
                        else result.invoke(false)
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        result.invoke(false)
                    }
                })
        }
    }

    fun requestSdPermission(activity: Activity, success: () -> Unit = {}, error: (Boolean) -> Unit = {}) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            XXPermissions.with(activity).permission(Permission.MANAGE_EXTERNAL_STORAGE)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) success.invoke()
                        else error.invoke(false)
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        error.invoke(doNotAskAgain)
                    }
                })
        } else {
            XXPermissions.with(activity)
                .permission(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>, allGranted: Boolean) {
                        if (allGranted) success.invoke()
                        else error.invoke(false)
                    }

                    override fun onDenied(permissions: MutableList<String>, doNotAskAgain: Boolean) {
                        error.invoke(doNotAskAgain)
                    }
                })
        }
    }

    fun openInstallPermission(activity: Activity, file: File) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mApkFile = file
            val pm: PackageManager = activity.packageManager
            if (pm.canRequestPackageInstalls()) {
                mApkFile?.let {
                    DeviceUtils.installAPK(it)
                }
            } else {
                //跳转到该应用的安装应用的权限页面
                val packageURI: Uri = Uri.parse("package:" + activity.packageName)
                //注意这个是8.0新API
                val intent = Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI)
                activity.startActivityForResult(intent, JumpConstants.OpenInstallPermissionCode)
            }
        } else {
            mApkFile?.let {
                DeviceUtils.installAPK(it)
            }
        }
    }

    fun onOpenInstallPermissionResult(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val pm: PackageManager = activity.packageManager
            if (pm.canRequestPackageInstalls()) {
                mApkFile?.let {
                    DeviceUtils.installAPK(it)
                }
            }
        }
    }
}